package com.ywq.app.web.mvc.misc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ywq.app.bean.ImageWeb;
import com.ywq.app.biz.ArtRoomBiz;
import com.ywq.app.biz.ImageBiz;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.ArtPic;
import com.ywq.app.domain.ArtRoom;
import com.ywq.app.domain.ArtRoomPic;
import com.ywq.app.domain.ArtRoomVideo;
import com.ywq.app.web.mvc.artRoom.ArtRoomController;
import com.ywq.app.web.mvc.test.CommonController;
import com.ywq.common.constant.Constant;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
import com.ywq.common.util.Encode;
@Controller
public class ArtRoomMiscController {
	private final static Logger logger = LoggerFactory.getLogger(ArtRoomController.class);
	@Autowired
	private ArtRoomBiz artRommBiz;
	@Autowired
	private ImageBiz imageBiz;
	
	/**
	 * 展览馆列表页面
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artRoom/artRoom.htm")
	public String artRoom(HttpServletRequest request,
				HttpServletResponse response) throws IOException {
			return "misc/artRoom/artRoom";
			}
	/**
	 * 展览馆展示列表链接
	 * @param page
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artRoom/artRoomGrid.htm")
	public String artPicGrid(Integer page,Integer pagesize, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
	
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			if (page == null) {
				page = 1;
			}
			PageModel pageModelUser = artRommBiz.getArtRoomListForMisc(page,pagesize);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(pageModelUser));
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList failure.", e);
			}
		}
		return null;
	}
	/**
	 * 添加展览馆页面
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artRoom/addArtRoomWeb.htm")
	public String addArtPicWeb(String roomId,HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "misc/artRoom/addArtRoomWeb";
	}
	/**
	 * 修改展览馆页面
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artRoom/updateRoomWeb.htm")
	public String updateRoomWeb(String roomId,HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		try {
			ArtRoom artRoom =artRommBiz.getRoomById(roomId);
			List<ArtArtist> artistList=artRommBiz.getArtArtistList(roomId);
			request.setAttribute("artistList", artistList);
			request.setAttribute("room", artRoom);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "misc/artRoom/updateRoomWeb";
	}
	/**
	 * 添加展览馆
	 * @param request
	 * @param response
	 * @param artPic
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artRoom/insertArtRoom.htm", method = RequestMethod.POST)
	public String insertArtRoom(HttpServletRequest request,
			HttpServletResponse response, ArtRoom artRoom) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			String escaped_json = request.getParameter("jsonData");// 获取提交的参数值,jsonData与上面红体字的jsonData相对应
            String json = Encode.unescape(escaped_json);// 将得到的json,进行unescape解析,(Encode是我自定义的类)
            JSONArray jsonArray = JSONArray.fromObject(json);
            @SuppressWarnings({ "unchecked", "static-access" })
            ArrayList<ArtArtist> artistList = (ArrayList<ArtArtist>) jsonArray.toCollection(jsonArray, ArtArtist.class);
			artRommBiz.insertArtRoom(artRoom,artistList);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("insertArtPic failure.", e);
			}
		}
		return null;
	}
	//
	@RequestMapping(value = "/misc/artRoom/updateArtRoom.htm", method = RequestMethod.POST)
	public String updateArtRoom(HttpServletRequest request, HttpServletResponse response, ArtRoom artRoom) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			String escaped_json = request.getParameter("jsonData");// 获取提交的参数值,jsonData与上面红体字的jsonData相对应
            String json = Encode.unescape(escaped_json);// 将得到的json,进行unescape解析,(Encode是我自定义的类)
            JSONArray jsonArray = JSONArray.fromObject(json);
            @SuppressWarnings({ "unchecked", "static-access" })
            ArrayList<ArtArtist> artistList = (ArrayList<ArtArtist>) jsonArray.toCollection(jsonArray, ArtArtist.class);
			artRommBiz.updateRoom(artRoom,artistList);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("insertArtPic failure.", e);
			}
		}
		return null;
	}
	
	/**
	 * 展览馆详情
	 * @param roomId
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/index/showRoomDetail.htm")
	public String showRoomDetail(String roomId, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
			try {
				List<ArtRoomPic> roomPicList =artRommBiz.getArtRoomListByRId(roomId);
				request.setAttribute("roomPicList", roomPicList);
			} catch (AppBusinessException e) {
				e.printStackTrace();
			}
		return "misc/artRoom/showRoomDetail";
	}
	/**
	 * 显示展览馆
	 * @param request
	 * @param response
	 * @param artRoom
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artRoom/displayRoom.htm", method = RequestMethod.POST)
	public String displayRoom(HttpServletRequest request,HttpServletResponse response, ArtRoom artRoom) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artRoom.setIsShow(true);
			artRommBiz.updateArtRoom(artRoom);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("updateArtRoom failure.", e);
			}
		}
		return null;
	}
	
	/**
	 * 关闭展览馆
	 * @param request
	 * @param response
	 * @param artRoom
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artRoom/hideRoom.htm", method = RequestMethod.POST)
	public String hideRoom(HttpServletRequest request,HttpServletResponse response, ArtRoom artRoom) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artRoom.setIsShow(false);
			artRommBiz.updateArtRoom(artRoom);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("hideRoom failure.", e);
			}
		}
		return null;
	}
	/**
	 * 某展览馆的作品集
	 * 
	 */
	@RequestMapping(value = "/misc/index/roomPicListGrid.htm")
	public String roomPicListGrid(Integer page,Integer pagesize, String roomId,HttpServletRequest request, HttpServletResponse response) throws IOException {
	
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			if (page == null) {
				page = 1;
			}
			PageModel pageModel= artRommBiz.getRoomPicListGrid(roomId,page,pagesize);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(pageModel));
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList failure.", e);
			}
		}
		return null;
	}
	//删除展览馆作品
	@RequestMapping(value = "/misc/index/deleteRoomPic.htm", method = RequestMethod.POST)
	public String hideRoom(String picId,String roomId,HttpServletRequest request,HttpServletResponse response, ArtRoom artRoom) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artRommBiz.deleteRoomPic(picId,roomId);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("hideRoom failure.", e);
			}
		}
		return null;
	}
	//删除展览馆
	@RequestMapping(value = "/misc/artRoom/deleteRoom.htm", method = RequestMethod.POST)
	public String deleteRoom(String roomId,HttpServletRequest request,HttpServletResponse response, ArtRoom artRoom) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artRommBiz.deleteRoom(roomId);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("hideRoom failure.", e);
			}
		}
		return null;
	}
	@RequestMapping(value = "/misc/artRoom/addFlv.htm")
	public String addFlv(String roomId,HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		request.setAttribute("roomId", roomId);
		return "misc/artPic/addFlv";
	}
	//保存视频
	@RequestMapping(value = "/misc/artPic/uploadFlv.htm")
	public String uploadFlv(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8"); 
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = response.getWriter(); 
		try {
			String imageDir=getRootPath();
			String flvUrl = imageBiz.uploadFlv(request, Constant.IMAGE_PATH_FLV,imageDir);
			Map<String,Object> map =new HashMap<String,Object>();
			if(!"".equals(flvUrl)){
				map.put("flvUrl", flvUrl);
				map.put("date", "true");
				pw.print(JSONObject.fromObject(map));
			}else{
				map.put("date", "false");
				pw.print(JSONObject.fromObject(map));
			}
			
		} catch (Exception e) {
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("Add user failure.", e);
			}
		}
		return null;
	} 
	 public static String getRootPath(){   
	        //因为类名为"CommonController"，因此" CommonController.class"一定能找到   
	        String result = CommonController.class.getResource("CommonController.class").toString();   
	        int index = result.indexOf("WEB-INF");   
	        if(index == -1){   
	            index = result.indexOf("bin");   
	        }   
	        result = result.substring(0,index);   
	        if(result.startsWith("jar")){   
	            // 当class文件在jar文件中时，返回"jar:file:/F:/ ..."样的路径    
	            result = result.substring(10);   
	        }else if(result.startsWith("file")){   
	            // 当class文件在class文件中时，返回"file:/F:/ ..."样的路径    
	            result = result.substring(6);   
	        }   
	        if(result.endsWith("/"))result = result.substring(0,result.length()-1);//不包含最后的"/"   
	        return result;   
	    }
	//保存视频
	 @RequestMapping(value = "/misc/artPic/saveFlv.htm", method = RequestMethod.POST)
		public String insertArtFlv(HttpServletRequest request,
				HttpServletResponse response, ArtRoomVideo artRoomVideo) throws IOException {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = null;
			try {
				artRommBiz.insertArtFlv(artRoomVideo);
				pw = response.getWriter();
				pw.print("success");
			} catch (Exception e) {
				pw = response.getWriter();
				pw.print("error");
				if (logger.isErrorEnabled()) {
					logger.error("insertArtPic failure.", e);
				}
			}
			return null;
		}
	//视频列表
	 @RequestMapping(value = "/misc/artRoom/flvList.htm")
		public String flvList(HttpServletRequest request, HttpServletResponse response) throws IOException {
			return "misc/artRoom/flvList";
		}
	 //视频遍历
	 @RequestMapping(value = "/misc/artRoom/flvListGrid.htm")
		public String flvListGrid(Integer page,Integer pagesize, HttpServletRequest request,
				HttpServletResponse response) throws IOException {
		
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = null;
			try {
				if (page == null) {
					page = 1;
				}
				PageModel pageModel  = artRommBiz.getArtRoomFlv(page,pagesize);
				pw = response.getWriter();
				pw.print(JSONObject.fromObject(pageModel));
			} catch (Exception e) {
				pw = response.getWriter();
				pw.print("error");
				if (logger.isErrorEnabled()) {
					logger.error("flvListGrid failure.", e);
				}
			}
			return null;
		}
	 //删除flv
	 @RequestMapping(value = "/misc/artRoom/deleteFlv.htm", method = RequestMethod.POST)
		public String deleteFlv(String id,HttpServletRequest request,HttpServletResponse response, ArtRoom artRoom) throws IOException {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = null;
			try {
				artRommBiz.deleteFlv(id);
				pw = response.getWriter();
				pw.print("success");
			} catch (Exception e) {
				pw = response.getWriter();
				pw.print("error");
				if (logger.isErrorEnabled()) {
					logger.error("deleteFlv failure.", e);
				}
			}
			return null;
		}
}
